package club.haomoon.boss.service.impl;

import club.haomoon.boss.service.ISettingPaymentService;
import club.haomoon.boss.validate.base.SettingPayConfigValidate;
import club.haomoon.boss.vo.base.SettingPaymentMethodVo;
import club.haomoon.core.entity.system.PayConfigData;
import club.haomoon.core.entity.system.PayWayData;
import club.haomoon.core.enums.PaymentEnum;
import club.haomoon.core.mapper.system.PayConfigDataMapper;
import club.haomoon.core.mapper.system.PayWayDataMapper;
import club.haomoon.core.util.UrlUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import jakarta.annotation.Resource;
import java.util.*;

/**
 * -------------------------
 * @Description: 支付配置服务实现类
 * @Author: Wangjianwei
 * @Date: 2025/02/17 10:22
 * -------------------------
 */
@Service
public class SettingPaymentServiceImpl implements ISettingPaymentService {

    @Resource
    PayConfigDataMapper payConfigDataMapper;

    @Resource
    PayWayDataMapper payWayDataMapper;

    /**
     * 支付配置方式
     * @return List<List<SettingPaymentMethodVo>>
     */
    public HashMap getPayWay() {
        List<PayWayData> lists = payWayDataMapper.selectList(null);

        //List<List<SettingPaymentMethodVo>> result = new LinkedList<>();
        HashMap result = new LinkedHashMap<Integer, String>();
        List<SettingPaymentMethodVo> mnp = new LinkedList<>();
        List<SettingPaymentMethodVo> oa = new LinkedList<>();
        List<SettingPaymentMethodVo> h5 = new LinkedList<>();

        for (PayWayData devPayWay : lists) {
            PayConfigData devPayConfig = payConfigDataMapper.selectOne(
                    new QueryWrapper< PayConfigData>()
                        .eq("id", devPayWay.getPayConfigId())
                        .last("limit 1"));

            SettingPaymentMethodVo vo = new SettingPaymentMethodVo();
            BeanUtils.copyProperties(devPayWay, vo);
            vo.setPayWayName(PaymentEnum.getPayWayMsg(devPayConfig.getPayWay()));
            vo.setIcon(UrlUtil.toAdminAbsoluteUrl(devPayConfig.getIcon()));

            switch (devPayWay.getScene()) {
                case 1: // 微信小程序
                    mnp.add(vo);
                    break;
                case 2: // 微信公众号
                    oa.add(vo);
                    break;
                case 3:
                    h5.add(vo);
                    break;
            }
        }

        result.put(1, mnp);
        result.put(2, oa);
        result.put(3, h5);
        return result;
    }

    /**
     * 支付配置列表
     * @return List<PayConfigData>
     */
    @Override
    public JSONObject list() {
        List<PayConfigData> payConfigDataList = payConfigDataMapper.selectList(
                new QueryWrapper<PayConfigData>()
                    .orderByDesc(Arrays.asList("sort", "id")));

        for (PayConfigData dev : payConfigDataList) {
            dev.setPayWayName(PaymentEnum.getPayWayMsg(dev.getPayWay()));
            dev.setName(dev.getName());
            dev.setConfig(JSONObject.parse("{}"));
            dev.setIcon(UrlUtil.toAdminAbsoluteUrl(dev.getIcon()));
        }

        JSONObject result = new JSONObject() {{
            put("lists", payConfigDataList);
        }};
        return result;
    }

    /**
     * 支付配置详情
     * @param id 主键
     * @return PayConfigData
     */
    @Override
    public PayConfigData getConfig(Integer id) {
        PayConfigData payConfigData = payConfigDataMapper.selectOne(
                new QueryWrapper<PayConfigData>()
                        .eq("id", id)
                        .last("limit 1"));

        payConfigData.setName(PaymentEnum.getPayWayMsg(payConfigData.getPayWay()));
        payConfigData.setPayWayName(payConfigData.getName());
        payConfigData.setConfig(JSONObject.parse(payConfigData.getConfig().toString()));
        payConfigData.setIcon(UrlUtil.toAdminAbsoluteUrl(payConfigData.getIcon()));
        return payConfigData;
    }

    /**
     * 支付配置编辑
     * @param configValidate 参数
     */
    @Override
    public void setConfig(SettingPayConfigValidate configValidate) {
        PayConfigData payConfigData = payConfigDataMapper.selectOne(
                new QueryWrapper<PayConfigData>()
                        .eq("id", configValidate.getId())
                        .last("limit 1"));

        Assert.notNull(payConfigData, "数据不存在!");

        payConfigData.setName(configValidate.getName());
        payConfigData.setIcon(UrlUtil.toRelativeUrl(configValidate.getIcon()));
        payConfigData.setSort(configValidate.getSort());
        payConfigData.setRemark(configValidate.getRemark());
        if (payConfigData.getPayWay().equals(1)) {
            payConfigData.setConfig("{}");
        } else {
            payConfigData.setConfig(configValidate.getConfig().toJSONString());
        }
        payConfigDataMapper.updateById(payConfigData);
    }

    /**
     * 支付方式编辑
     * @param data 参数
     */
    @Override
    @Transactional
    public void setPayWay(HashMap<Integer, List<SettingPaymentMethodVo>> data) {
        for (Integer key : data.keySet()) {
            List<SettingPaymentMethodVo> list = data.get(key);
            for (SettingPaymentMethodVo vo : list) {
                PayWayData way = new PayWayData();
                way.setIsDefault(vo.getIsDefault());
                way.setStatus(vo.getStatus());
                payWayDataMapper.update(way, new QueryWrapper<PayWayData>().eq("id", vo.getId()));
            }
        }
    }

}
